<?php

class UserController extends Controller {

    private $_model;

    public $layout='application.views.layouts.column1';

    public function filters() {
        return array(
                //'accessControl', // perform access control for CRUD operations

        );
    }

    public function accessRules() {

        return array(
                array('allow',
                        'actions'=>array('login','logout'),
                        'users'=>array('*'),
                ),

                array('deny',  // deny all users
                        'users'=>array('*'),
                ),
        );
    }



    public function actionLogin() {
        $form=new LoginForm('login');
        // collect user input data
        if(isset($_POST['LoginForm'])) {
            $form->attributes=$_POST['LoginForm'];
            // validate user input and redirect to previous page if valid
            if($form->validate() && $form->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login',array('model'=>$form));

    }

    public function actionLogout() {
                Yii::app()->user->logout();
        $this->redirect(Yii::app()->user->returnUrl);
    }

    public function actionRegistration() {
        $model = new User('registration');

        if (!empty($_POST['User'])) {

            $model->attributes=$_POST['User'];

            if(User::model()->checkName($model->name)) {
                $model->addError('login','Пользователь с таким именнем существует');
                $this->render("registration", array('model' => $model));
                return;
            }
            if(User::model()->checkMail($model->mail)) {
                $model->addError('login','Выберите другой почтовый адресс');
                $this->render("registration", array('model' => $model));
                return;
            }

            if($model->validate()&&!User::model()->checkName($model->name)) {

                $model->password = md5('asdfasd'.$model->password.'sd1234');
                $model->password2 = $model->password;
                $model->token = md5('thistockin'.$model->name);
                $model->save();
                $this->render('registration_ok',array('data'=>$model->token));
                return;
            }
        }
        $this->render('registration', array('model' => $model));
    }
    public function actionActivation(){
        if(isset($_GET['id'])){
            $rec = User::model()->find('token=:token', array(':token'=>$_GET['id']))->id_user;
            User::model()->updateByPk($rec, array('activation'=>1));
            $this->render('registration_end');
        }else{$this->redirect(Yii::app()->user->returnUrl);
        }
    }
    public function actionInfo() {
    }

}